<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/blog/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="数据结构,读书笔记," />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="二叉搜索树概念二叉搜索树又称二叉查找树，如果非空，它具有如下特性：  如果它的左子树非空，则左子树上所有结点的关键字均小于根节点的关键字； 如果它的右子树非空，则右子树上所有结点的关键字均大于根节点的关键字； 左、右子树本身又各是一棵二叉搜索树  二叉搜索树是二叉树加上一些限制条件所形成的，二叉搜索树常被用来建立一棵树状结构的程序。二叉搜索树的特征：  在左子树的所有键值均小于树根的键值。 在右子">
<meta name="keywords" content="数据结构,读书笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="二叉搜索树">
<meta property="og:url" content="https://git.oschina.net/choichumgming/blog/2017/12/26/二叉搜索树/index.html">
<meta property="og:site_name" content="choichumgming&#39;s blog">
<meta property="og:description" content="二叉搜索树概念二叉搜索树又称二叉查找树，如果非空，它具有如下特性：  如果它的左子树非空，则左子树上所有结点的关键字均小于根节点的关键字； 如果它的右子树非空，则右子树上所有结点的关键字均大于根节点的关键字； 左、右子树本身又各是一棵二叉搜索树  二叉搜索树是二叉树加上一些限制条件所形成的，二叉搜索树常被用来建立一棵树状结构的程序。二叉搜索树的特征：  在左子树的所有键值均小于树根的键值。 在右子">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-12-26T10:12:36.802Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="二叉搜索树">
<meta name="twitter:description" content="二叉搜索树概念二叉搜索树又称二叉查找树，如果非空，它具有如下特性：  如果它的左子树非空，则左子树上所有结点的关键字均小于根节点的关键字； 如果它的右子树非空，则右子树上所有结点的关键字均大于根节点的关键字； 左、右子树本身又各是一棵二叉搜索树  二叉搜索树是二叉树加上一些限制条件所形成的，二叉搜索树常被用来建立一棵树状结构的程序。二叉搜索树的特征：  在左子树的所有键值均小于树根的键值。 在右子">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://git.oschina.net/choichumgming/blog/2017/12/26/二叉搜索树/"/>





  <title>二叉搜索树 | choichumgming's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">choichumgming's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">stay foolish,stay hard</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/blog/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      
        
        <li class="menu-item menu-item-board">
          <a href="/blog/board/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://git.oschina.net/choichumgming/blog/blog/2017/12/26/二叉搜索树/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="choichumgming">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="choichumgming's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">二叉搜索树</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-12-26T15:41:46+08:00">
                2017-12-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/算法与数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">算法与数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/blog/2017/12/26/二叉搜索树/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2017/12/26/二叉搜索树/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <h1 id="二叉搜索树"><a href="#二叉搜索树" class="headerlink" title="二叉搜索树"></a>二叉搜索树</h1><h2 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h2><p>二叉搜索树又称二叉查找树，如果非空，它具有如下特性：</p>
<ol>
<li>如果它的左子树非空，则左子树上所有结点的关键字均小于根节点的关键字；</li>
<li>如果它的右子树非空，则右子树上所有结点的关键字均大于根节点的关键字；</li>
<li>左、右子树本身又各是一棵二叉搜索树</li>
</ol>
<p>二叉搜索树是二叉树加上一些限制条件所形成的，二叉搜索树常被用来建立一棵树状结构的程序。<br><strong>二叉搜索树的特征：</strong></p>
<ul>
<li>在<strong>左子树</strong>的所有键值均<strong>小于树根</strong>的键值。</li>
<li>在<strong>右子树</strong>的所有键值均<strong>大于树根</strong>的键值。</li>
<li>左子树和右子树也是二叉搜索树。<ul>
<li>每个键值都不一样</li>
</ul>
</li>
</ul>
<h2 id="插入与更新"><a href="#插入与更新" class="headerlink" title="插入与更新"></a>插入与更新</h2><p>二叉搜索树的插入和更新很简单，根据二叉搜索树的特征，插入某一值只要逐一比较，依据键值的大小往右或往左，便可找到此键值欲插入的位置。<br><strong>插入更新节点代码</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> data)</span></span>&#123;</div><div class="line">            BSTNode temp=<span class="keyword">new</span> BSTNode();</div><div class="line">            temp.value=data;</div><div class="line">            <span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="comment">//先考虑根是否为空</span></div><div class="line">            &#123;</div><div class="line">                root=temp;<span class="keyword">return</span>;</div><div class="line">            &#125;</div><div class="line">            BSTNode ptr=root,parent=<span class="keyword">null</span>;</div><div class="line">            <span class="keyword">while</span>(ptr!=<span class="keyword">null</span>)&#123;<span class="comment">//循环一直找到叶子节点</span></div><div class="line">                parent=ptr;</div><div class="line">                <span class="keyword">if</span>(ptr.value&gt;temp.value)</div><div class="line">                    ptr=ptr.left;</div><div class="line">                <span class="keyword">else</span> ptr=ptr.right;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(data&gt;parent.value) parent.right=temp;</div><div class="line">            <span class="keyword">else</span> parent.left=temp;</div><div class="line">        &#125;</div></pre></td></tr></table></figure></p>
<h2 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h2><p>删除某一节点时，若<strong>删除的是树叶节点，则直接删除之</strong>，假若删除不是树叶节点，则在<strong>左子树找一最大的节点</strong>或在<strong>右子树找一最小的节点</strong>，取代将被删除的节点<br> 现在会出现三种情况：</p>
<pre><code>1. 无孩子节点
2. 有一个孩子节点：只有左孩子/只有右孩子                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
3. 有两个孩子节点
</code></pre><p><strong>删除节点的代码:</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div></pre></td><td class="code"><pre><div class="line"><span class="function">BSTNode <span class="title">remove</span><span class="params">(<span class="keyword">int</span> key)</span></span>&#123;</div><div class="line">            BSTNode ptr=root,parent=<span class="keyword">null</span>;</div><div class="line">            <span class="keyword">while</span>(ptr!=<span class="keyword">null</span>&amp;&amp;ptr.value!=key) <span class="comment">//判断是否有值为key的叶子节点，还有与key值相等的叶子节点</span></div><div class="line">            &#123;</div><div class="line">                parent=ptr;</div><div class="line">                <span class="keyword">if</span>(ptr.value&gt;key)</div><div class="line">                    ptr=ptr.left;</div><div class="line">                <span class="keyword">else</span> ptr=ptr.right;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(ptr==<span class="keyword">null</span>)&#123;<span class="comment">//如果不存在值为key的叶子节点</span></div><div class="line">                <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">/*</span></div><div class="line"><span class="comment">             * 现在会出现三种情况：</span></div><div class="line"><span class="comment">             * 1.无孩子节点</span></div><div class="line"><span class="comment">             * 2.有一个孩子节点</span></div><div class="line"><span class="comment">             *  只有左孩子</span></div><div class="line"><span class="comment">             *  只有右孩子                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   </span></div><div class="line"><span class="comment">             * 3.有两个孩子节点</span></div><div class="line"><span class="comment">             */</span></div><div class="line">            <span class="keyword">if</span>(ptr.left==<span class="keyword">null</span> &amp;&amp; ptr.right==<span class="keyword">null</span> )&#123;<span class="comment">//这时是无孩子节点的情况</span></div><div class="line">                 Assign(parent,ptr,<span class="keyword">null</span>);<span class="comment">//因为没有孩子节点，所以直接把替代的值设置为空就好了</span></div><div class="line">                 <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">//这时是有一个孩子节点的情况</span></div><div class="line">            <span class="keyword">if</span>(ptr.left==<span class="keyword">null</span>)</div><div class="line">            &#123;</div><div class="line">                Assign(parent,ptr,ptr.right);</div><div class="line">                <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(ptr.right==<span class="keyword">null</span>)</div><div class="line">            &#123;</div><div class="line">                Assign(parent,ptr,ptr.left);</div><div class="line">                <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">//这时是有两个孩子节点的情况</span></div><div class="line">            BSTNode rptr=ptr.right,rparent=ptr;<span class="comment">//如果有两个孩子，就让右孩子的最后一个左孩子作为替代（就是中序遍历中，被remove掉的节点后面的第一个节点）</span></div><div class="line">            <span class="keyword">while</span>(rptr.left!=<span class="keyword">null</span>)<span class="comment">//找到</span></div><div class="line">            &#123;</div><div class="line">                rparent=rptr;</div><div class="line">                rptr=rptr.left;</div><div class="line">            &#125;</div><div class="line">            Assign(rparent,rptr,rptr.right);<span class="comment">//先把这个点从原来的位置移除，这个操作只会改变这个节点双亲节点的孩子的引用，不会对他本身造成影响。能找到这，说明这个节点肯定是已经没有左孩子了，右孩子不确定有没有，如果右孩子有的话按第二种情况处理，反之则按第一种</span></div><div class="line">            rptr.left=ptr.left;<span class="comment">//将当前节点的孩子节点的引用传给 替代当前节点 的节点 的孩子节点</span></div><div class="line">            rptr.right=ptr.right;</div><div class="line">            Assign(parent,ptr,rptr);<span class="comment">//最后再将当前节点的双亲节点的孩子节点指向 替代当前节点的节点</span></div><div class="line">            <span class="keyword">return</span> ptr;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">void</span> <span class="title">Assign</span><span class="params">(BSTNode parent,BSTNode old,BSTNode newnode)</span></span>&#123;</div><div class="line">            <span class="keyword">if</span>(parent==<span class="keyword">null</span>) root=newnode;</div><div class="line">            <span class="keyword">else</span>&#123;</div><div class="line">                <span class="keyword">if</span>(old==parent.left)parent.left=newnode;<span class="comment">//判断被删除的值是父亲节点的左孩子还是右孩子</span></div><div class="line">                <span class="keyword">else</span> parent.right=newnode;</div><div class="line">            &#125;</div><div class="line">        &#125;</div></pre></td></tr></table></figure></p>
<p><strong>完整代码</strong><br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> BineryTree;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.util.Scanner;</div><div class="line"></div><div class="line"><span class="keyword">import</span> javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BST</span> </span>&#123;</div><div class="line">        BSTNode root;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">int</span> [] sequence;</div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">BST</span><span class="params">()</span> </span>&#123;</div><div class="line">            </div><div class="line">            </div><div class="line">             Scanner sc=<span class="keyword">new</span> Scanner(System.in);</div><div class="line">                <span class="keyword">int</span> num=sc.nextInt();</div><div class="line">                sc.nextLine();</div><div class="line">                <span class="keyword">while</span>(num--&gt;<span class="number">0</span>)&#123;</div><div class="line">                    root=<span class="keyword">null</span>;</div><div class="line">                    String[] psequence=sc.nextLine().split(<span class="string">" "</span>); </div><div class="line">                    sequence=change(psequence);</div><div class="line">                    <span class="keyword">for</span>(<span class="keyword">int</span> a:sequence) insert(a);</div><div class="line">                    </div><div class="line">                    MidOrder(root);</div><div class="line">                    System.out.println();</div><div class="line">                    PreOrder(root);</div><div class="line">                    </div><div class="line">                &#125;</div><div class="line"><span class="comment">//          remove(66);</span></div><div class="line"><span class="comment">//          MidOrder(root);</span></div><div class="line">        &#125;</div><div class="line">        <span class="keyword">int</span>[] change(String[] string)&#123;</div><div class="line">            <span class="keyword">int</span> []arr=<span class="keyword">new</span> <span class="keyword">int</span>[string.length];</div><div class="line">            <span class="keyword">int</span> num=<span class="number">0</span>;</div><div class="line">            <span class="keyword">for</span>(String s:string )&#123;</div><div class="line">                arr[num++]=Integer.parseInt(s, <span class="number">10</span>);</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> arr;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">void</span> <span class="title">insert</span><span class="params">(<span class="keyword">int</span> data)</span></span>&#123;</div><div class="line">            BSTNode temp=<span class="keyword">new</span> BSTNode();</div><div class="line">            temp.value=data;</div><div class="line">            <span class="keyword">if</span>(root==<span class="keyword">null</span>)<span class="comment">//先考虑根是否为空</span></div><div class="line">            &#123;</div><div class="line">                root=temp;<span class="keyword">return</span>;</div><div class="line">            &#125;</div><div class="line">            BSTNode ptr=root,parent=<span class="keyword">null</span>;</div><div class="line">            <span class="keyword">while</span>(ptr!=<span class="keyword">null</span>)&#123;<span class="comment">//循环一直找到叶子节点</span></div><div class="line">                parent=ptr;</div><div class="line">                <span class="keyword">if</span>(ptr.value&gt;temp.value)</div><div class="line">                    ptr=ptr.left;</div><div class="line">                <span class="keyword">else</span> ptr=ptr.right;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(data&gt;parent.value) parent.right=temp;</div><div class="line">            <span class="keyword">else</span> parent.left=temp;</div><div class="line">            </div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function">BSTNode <span class="title">remove</span><span class="params">(<span class="keyword">int</span> key)</span></span>&#123;</div><div class="line">            BSTNode ptr=root,parent=<span class="keyword">null</span>;</div><div class="line">            <span class="keyword">while</span>(ptr!=<span class="keyword">null</span>&amp;&amp;ptr.value!=key) <span class="comment">//判断是否有值为key的叶子节点，还有与key值相等的叶子节点</span></div><div class="line">            &#123;</div><div class="line">                parent=ptr;</div><div class="line">                <span class="keyword">if</span>(ptr.value&gt;key)</div><div class="line">                    ptr=ptr.left;</div><div class="line">                <span class="keyword">else</span> ptr=ptr.right;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(ptr==<span class="keyword">null</span>)&#123;<span class="comment">//如果不存在值为key的叶子节点</span></div><div class="line">                <span class="keyword">return</span> <span class="keyword">null</span>;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">/*</span></div><div class="line"><span class="comment">             * 现在会出现三种情况：</span></div><div class="line"><span class="comment">             * 1.无孩子节点</span></div><div class="line"><span class="comment">             * 2.有一个孩子节点</span></div><div class="line"><span class="comment">             *      ·只有左孩子</span></div><div class="line"><span class="comment">             *      ·只有右孩子                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       </span></div><div class="line"><span class="comment">             * 3.有两个孩子节点</span></div><div class="line"><span class="comment">             */</span></div><div class="line">            <span class="keyword">if</span>(ptr.left==<span class="keyword">null</span> &amp;&amp; ptr.right==<span class="keyword">null</span> )&#123;<span class="comment">//这时是无孩子节点的情况</span></div><div class="line">                 Assign(parent,ptr,<span class="keyword">null</span>);<span class="comment">//因为没有孩子节点，所以直接把替代的值设置为空就好了</span></div><div class="line">                 <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">//这时是有一个孩子节点的情况</span></div><div class="line">            <span class="keyword">if</span>(ptr.left==<span class="keyword">null</span>)</div><div class="line">            &#123;</div><div class="line">                Assign(parent,ptr,ptr.right);</div><div class="line">                <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span>(ptr.right==<span class="keyword">null</span>)</div><div class="line">            &#123;</div><div class="line">                Assign(parent,ptr,ptr.left);</div><div class="line">                <span class="keyword">return</span> ptr;</div><div class="line">            &#125;</div><div class="line">            </div><div class="line">            <span class="comment">//这时是有两个孩子节点的情况</span></div><div class="line">            BSTNode rptr=ptr.right,rparent=ptr;<span class="comment">//如果有两个孩子，就让右孩子的最后一个左孩子作为替代（就是中序遍历中，被remove掉的节点后面的第一个节点）</span></div><div class="line">            <span class="keyword">while</span>(rptr.left!=<span class="keyword">null</span>)<span class="comment">//找到</span></div><div class="line">            &#123;</div><div class="line">                rparent=rptr;</div><div class="line">                rptr=rptr.left;</div><div class="line">            &#125;</div><div class="line">            Assign(rparent,rptr,rptr.right);<span class="comment">//先把这个点从原来的位置移除，这个操作只会改变这个节点双亲节点的孩子的引用，不会对他本身造成影响。能找到这，说明这个节点肯定是已经没有左孩子了，右孩子不确定有没有，如果右孩子有的话按第二种情况处理，反之则按第一种</span></div><div class="line">            rptr.left=ptr.left;<span class="comment">//将当前节点的孩子节点的引用传给 替代当前节点 的节点 的孩子节点</span></div><div class="line">            rptr.right=ptr.right;</div><div class="line">            Assign(parent,ptr,rptr);<span class="comment">//最后再将当前节点的双亲节点的孩子节点指向 替代当前节点的节点</span></div><div class="line">            <span class="keyword">return</span> ptr;</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="function"><span class="keyword">void</span> <span class="title">Assign</span><span class="params">(BSTNode parent,BSTNode old,BSTNode newnode)</span></span>&#123;</div><div class="line">            <span class="keyword">if</span>(parent==<span class="keyword">null</span>) root=newnode;</div><div class="line">            <span class="keyword">else</span>&#123;</div><div class="line">                <span class="keyword">if</span>(old==parent.left)parent.left=newnode;<span class="comment">//判断被删除的值是父亲节点的左孩子还是右孩子</span></div><div class="line">                <span class="keyword">else</span> parent.right=newnode;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="function"><span class="keyword">void</span> <span class="title">MidOrder</span><span class="params">(BSTNode p)</span></span>&#123;<span class="comment">//中序输出</span></div><div class="line">            <span class="keyword">if</span>(p==<span class="keyword">null</span>)<span class="keyword">return</span>;</div><div class="line">            MidOrder(p.left);</div><div class="line">            System.out.print(p.value+<span class="string">" "</span>);</div><div class="line">            MidOrder(p.right);</div><div class="line">        &#125;</div><div class="line">        <span class="function"><span class="keyword">void</span> <span class="title">PreOrder</span><span class="params">(BSTNode p)</span></span>&#123;<span class="comment">//中序输出</span></div><div class="line">            <span class="keyword">if</span>(p==<span class="keyword">null</span>)<span class="keyword">return</span>;</div><div class="line">            PreOrder(p.left);</div><div class="line">            </div><div class="line">            PreOrder(p.right);</div><div class="line">            System.out.print(p.value+<span class="string">" "</span>);</div><div class="line">        &#125;</div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">            BST P=<span class="keyword">new</span> BST();</div><div class="line">        &#125;</div><div class="line">        </div><div class="line">        <span class="class"><span class="keyword">class</span> <span class="title">BSTNode</span></span>&#123;</div><div class="line">            <span class="keyword">int</span> value;</div><div class="line">            BSTNode left,right;</div><div class="line">            </div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/数据结构/" rel="tag"># 数据结构</a>
          
            <a href="/blog/tags/读书笔记/" rel="tag"># 读书笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2017/12/26/堆/" rel="next" title="堆">
                <i class="fa fa-chevron-left"></i> 堆
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2017/12/26/线性表/" rel="prev" title="线性表">
                线性表 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <!--PC版-->
<div id="SOHUCS" ></div>


    

  </div>
     <div class="comments" id="comments">
       
     </div>
   





        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.gif"
               alt="choichumgming" />
          <p class="site-author-name" itemprop="name">choichumgming</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="http://www.weibo.com/u/2558763801" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-weibo"></i>
                  
                    
                      微博
                    
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#二叉搜索树"><span class="nav-number">1.</span> <span class="nav-text">二叉搜索树</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#概念"><span class="nav-number">1.1.</span> <span class="nav-text">概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#插入与更新"><span class="nav-number">1.2.</span> <span class="nav-text">插入与更新</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#删除"><span class="nav-number">1.3.</span> <span class="nav-text">删除</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">choichumgming</span>

  
</div>


  <div class="powered-by">
    由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
  </div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">
    主题 &mdash;
    <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
      NexT.Gemini
    </a>
  </div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cytnT5dky';
      var conf = '56c170b7352bd027682e3707e9eb5071';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  





  





  

  

  

  

  

  

</body>
</html>
